﻿function UserStory(userStoryID, title, description, storyPoints, priority) {
    this.userStoryID = userStoryID;
    this.title = title;
    this.description = description;
    this.storyPoints = storyPoints;
    this.priority = priority;
}

function Sprint(sprintID, startDate, endDate, backlog) {
    this.sprintID = sprintID
    this.startDate = startDate;
    this.endDate = endDate;
    this.backlog = backlog;
}

function selectUserStory(list) {
    var editUserStory = document.getElementById("tbl_editUserStory");
    var createUserStory = document.getElementById("tbl_createUserStory");
    var selectedValue = list[list.selectedIndex].value;
    if (selectedValue == "!add_user_story!") {
        document.forms["createUserStory"]["Title"].value = "";
        document.forms["createUserStory"]["StoryPoints"].value = "";
        document.forms["createUserStory"]["Priority"].value = "";
        document.forms["createUserStory"]["Description"].value = "";
        editUserStory.style.display = "none";
        createUserStory.style.display = "block";
    }
    else {
        var index = list.selectedIndex - 1;
        selectedUserStory = userStories[index];
        document.forms["editUserStory"]["Title"].value = selectedUserStory.title;
        document.forms["editUserStory"]["StoryPoints"].value = selectedUserStory.storyPoints;
        document.forms["editUserStory"]["Priority"].value = selectedUserStory.priority;
        document.forms["editUserStory"]["Description"].value = selectedUserStory.description;
        document.forms["editUserStory"]["UserStoryID"].value = selectedUserStory.userStoryID;
        createUserStory.style.display = "none";
        editUserStory.style.display = "block";
    }
}

function resetUnassignedBackloglist() {
    unassignedBackloglist.innerHTML = "";
    var option;
    for (var i = 0; i < unassignedUserStories.length; i++) {
        var userStory = unassignedUserStories[i];
        option = document.createElement("option");
        option.setAttribute("value", userStory.userStoryID);
        option.innerHTML = userStory.title;
        unassignedBackloglist.appendChild(option);
    }
}

function addUserStories() {
    var unassignedBackloglist = document.getElementById("unassignedbackloglist");
    var sprintBackloglist = document.getElementById("sprintbackloglist" + mode);
    var selectedUserStories = new Array();

    var options = unassignedBackloglist.options;

    for (var i = 0; i < options.length; i++) {
        if (options[i].selected) {
            options[i].selected = false;
            selectedUserStories.push(options[i]);
        }
    }

    for (var i = 0; i < selectedUserStories.length; i++) {
        sprintBackloglist.add(selectedUserStories[i]);
    }
}

function removeUserStories() {
    var unassignedBackloglist = document.getElementById("unassignedbackloglist");
    var sprintBackloglist = document.getElementById("sprintbackloglist" + mode);
    var selectedUserStories = new Array();

    var options = sprintBackloglist.options;

    for (var i = 0; i < options.length; i++) {
        if (options[i].selected) {
            options[i].selected = false;
            selectedUserStories.push(options[i]);
        }
    }

    for (var i = 0; i < selectedUserStories.length; i++) {
        unassignedBackloglist.add(selectedUserStories[i]);
    }
}

function selectAll(list) {
    var options = list.options;
    for (var i = 0; i < options.length; i++) {
        options[i].selected = true;
    }
}

function selectSprint(list) {
    var editSprint = document.getElementById("tbl_editSprint");
    var createSprint = document.getElementById("tbl_createSprint");
    var selectedValue = list[list.selectedIndex].value;
    if (selectedValue == "!add_sprint!") {
        document.forms["createSprint"]["StartDate"].value = "";
        document.forms["createSprint"]["EndDate"].value = "";
        document.getElementById("sprintbackloglistcreate").innerHTML = "";
        editSprint.style.display = "none";
        createSprint.style.display = "block";
        mode = "create";
        resetUnassignedBackloglist();
    }
    else {
        var index = list.selectedIndex - 1;
        selectedSprint = sprints[index];
        backlog = selectedSprint.backlog;
        var sprintbackloglist = document.getElementById("sprintbackloglistedit");
        sprintbackloglist.innerHTML = "";
        var option;
        for (var i = 0; i < backlog.length; i++) {
            option = document.createElement("option");
            option.setAttribute("value", backlog[i].userStoryID);
            option.innerHTML = backlog[i].title;
            sprintbackloglist.add(option);
        }
        document.forms["editSprint"]["StartDate"].value = selectedSprint.startDate;
        document.forms["editSprint"]["EndDate"].value = selectedSprint.endDate;
        document.forms["editSprint"]["SprintID"].value = selectedSprint.sprintID;
        createSprint.style.display = "none";
        editSprint.style.display = "block";
        mode = "edit";
        resetUnassignedBackloglist();
    }
}

function deleteItem(item, url) {
    if (confirm('Are you sure you wish to delete this ' + item + '?')) {
        window.open(url, '_self');
    }
}